package com.cn.filter;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cn.domain.Category;
import com.cn.domain.Comment;
import com.cn.domain.News;
import com.cn.domain.Product;
import com.cn.service.CommentService;
import com.cn.service.NewsService;
import com.cn.service.ProductCategoryService;
import com.cn.service.ProductService;
import com.cn.service.impl.CommentServiceImpl;
import com.cn.service.impl.NewsServiceImpl;
import com.cn.service.impl.ProductCategoryServiceImpl;
import com.cn.service.impl.ProductServiceImpl;
import com.cn.utils.JsonMapper;
import com.cn.utils.PageView;

//@WebFilter("/index.jsp")//url地址,随便写
@WebFilter(urlPatterns={"/*"}
		)//url地址,随便写
public class InitGoodsFilter implements Filter{
	
	private NewsService newsService=new NewsServiceImpl();
	private CommentService cs=new CommentServiceImpl();
	private ProductCategoryService pcs=new ProductCategoryServiceImpl();
	private ProductService productService=new ProductServiceImpl();
	
	//获取json工具
	private JsonMapper binder=JsonMapper.nonDefaultMapper();
	
	@Override
	public void destroy() {}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		HttpServletRequest request=(HttpServletRequest)arg0;
		HttpServletResponse response=(HttpServletResponse)arg1;
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//对购物车界面和订单界面进行过滤
		String url=request.getRequestURI();//amazon/shopping.jsp
		System.out.println("urll==="+url);
		/*if("/amazon/shopping.jsp".equals(url) || "/amazon/all_orders_view.jsp".equals(url)){
			//调到登录界面
			request.getRequestDispatcher("/login.jsp").forward(request, response);
			return;
		}*/
		
		//新闻信息
		request.setAttribute("news",newsService.queryAllNews());
		
		//商品分类
		Map<String,Object> map=pcs.queryAllParent();
		List<Category> parentNode = (List<Category>)map.get("parentCategory");
		Map<Integer,List<Category>> childCategory = (Map<Integer,List<Category>>)map.get("childCategory");
		request.setAttribute("parentCategory", parentNode);
		request.setAttribute("childCategory", childCategory);
		
		//显示初始化的商品列表
		//当前总记录条数
		int currentPage=1;
		//先给顶默认是父节点的产品列出来
		List<Product> productList=pcs.queryProductById("parent","2",currentPage,PageView.PAGE_SIZE);
		//获取总条数
		List<Product> productList2=pcs.queryAllProductById("parent","2");
		
		
		//这里进行分页显示
		PageView pageView=new PageView(currentPage, productList2.size(), productList);
		pageView.setStatus(1);
		
		String str=(String) request.getSession().getAttribute("hpCID");
		if(str==null){
			request.getSession().setAttribute("hpCID","2");
		}
		request.setAttribute("pageModel", pageView);
		
		
		//获取热卖商品,初始值可以直接给5张图片显示热卖
		List<Product> hotProList=productService.queryAllCart(5);
		request.setAttribute("hotProducts", hotProList);
		
		
		//取出浏览记录proInfo属性中
		Cookie[] cookies=request.getCookies();
		if(cookies!=null && cookies.length>0) {
			//开始商品进行展示
			for(Cookie cookie:cookies) {
				if("proInfo".equals(cookie.getName())){
					//找到了这个cookie,然后开始展示
					String json=cookie.getValue();
					//进行解码
					LinkedList<Product> pro=binder.fromJson(URLDecoder.decode(cookie.getValue(),"UTF-8"),binder.contructCollectionType(LinkedList.class,Product.class));
					request.getSession().setAttribute("viewedProduct", pro);
					break;
				}
			}
		}
		//放行
		arg2.doFilter(arg0, arg1);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {}
}
